/* This file is part of FireEscape.
 *
 * FireEscape is the intellectual work of Phillip Cheng & Allen Park.
 * All future releases or modifications of this program must include this
 * attribuation as according to the Creative Commons BY-SA license.
 *
 * FireEscape is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * FireEscape is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FireEscape.  If not, see <http://www.gnu.org/licenses/>.
 *
 * All modifications to this program should include this original statement.
 *
 * If you modify, distribute, or use the program , please contact
 * fireescape@kloudvine.com
 * http://code.google.com/p/fireescape/
 *
 * If you are interested in using FireEscape for your structure, please contact us
 * for support.
 *
 * If you do use FireEscape, please consider donating to the project.
 */
package fireescape;

import java.awt.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ServerSocketFactory;

/**
 * The server for logging
 * @author drtechmaster
 */
public class LogServer {

    private static String logFile;
    private static final int PORT_NUM = 5000;
    //private static BufferedWriter out;
    private static ArrayList<Double> data = new ArrayList<Double>();
    private static ArrayList<String> cIDs = new ArrayList<String>();
    private static ArrayList<Integer> cScores = new ArrayList<Integer>();

    /**
     *
     * @param args The command line arguments
     */
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Where to dump the log?: ");
        logFile = in.nextLine();
        ServerSocketFactory serverSocketFactory = ServerSocketFactory.getDefault();
        ServerSocket serverSocket = null;
        try {
            serverSocket =
                    serverSocketFactory.createServerSocket(PORT_NUM);
        } catch (IOException ex) {
            ex.printStackTrace();
            System.err.println("Unable to create server");
            System.exit(-1);
        }
        System.out.printf("LogServer running on port: %s%n", PORT_NUM);
        while (true) {
            Socket socket = null;
            try {
                new LoggerHandler(socket = serverSocket.accept()).start();
                //System.out.println("Accepted");
            } catch (IOException ex) {
                //Handling next client because client on other end most likely dc'd
            }
        }
    }

    /**
     * Writes a line
     * @param s The line that is being written
     */
    public static synchronized void writeLine(String s) {
        try {
            System.out.println(s);
            System.out.flush();
            BufferedWriter outa = new BufferedWriter(new FileWriter(logFile+".log", true));
            outa.newLine();
            outa.write(s);
            outa.flush();
            outa.close();
            outa = null;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * Prints out the data
     * @return The data
     */
    public static synchronized ArrayList<Double> returnData() {
        for (Double d : data) {
            System.out.println(d);
        }
        return data;
    }
}


